<template>
  <view class="favorites-container">
    <!-- 顶部切换栏 -->
    <view class="tab-header">
      <view 
        class="tab-item" 
        :class="{ active: activeTab === 'albums' }"
        @click="activeTab = 'albums'"
      >
        <text>相册收藏</text>
        <view class="tab-line" v-if="activeTab === 'albums'"></view>
      </view>
      <view 
        class="tab-item" 
        :class="{ active: activeTab === 'photos' }"
        @click="activeTab = 'photos'"
      >
        <text>照片收藏</text>
        <view class="tab-line" v-if="activeTab === 'photos'"></view>
      </view>
    </view>
    
    <!-- 相册收藏内容 -->
    <scroll-view 
      v-if="activeTab === 'albums'"
      class="content-scroll"
      scroll-y
      @scrolltolower="loadMore"
      refresher-enabled
      :refresher-triggered="isRefreshing"
      @refresherrefresh="onRefresh"
    >
      <view class="albums-grid">
        <view 
          class="album-item" 
          v-for="album in favoriteAlbums" 
          :key="album.id"
          @click="openAlbum(album)"
        >
          <image class="album-cover" :src="album.cover" mode="aspectFill"></image>
          <view class="album-info">
            <text class="album-name">{{album.name}}</text>
            <view class="album-meta">
              <text class="photo-count">{{album.photoCount}}张照片</text>
              <text class="collect-time">{{album.collectTime}}</text>
            </view>
          </view>
          <view class="album-author">
            <image class="author-avatar" :src="album.authorAvatar" mode="aspectFill"></image>
            <text class="author-name">{{album.authorName}}</text>
          </view>
        </view>
      </view>
      
      <!-- 加载更多提示 -->
      <view class="loading-more" v-if="isLoading">
        <u-loading-icon mode="circle" size="24" color="#999"></u-loading-icon>
        <text>加载中...</text>
      </view>
    </scroll-view>
    
    <!-- 照片收藏内容 -->
    <scroll-view 
      v-if="activeTab === 'photos'"
      class="content-scroll"
      scroll-y
      @scrolltolower="loadMore"
      refresher-enabled
      :refresher-triggered="isRefreshing"
      @refresherrefresh="onRefresh"
    >
      <view class="photos-waterfall">
        <view class="waterfall-column">
          <view 
            class="photo-item" 
            v-for="photo in leftPhotos" 
            :key="photo.id"
            @click="previewPhoto(photo)"
          >
            <image :src="photo.url" mode="widthFix"></image>
            <view class="photo-info">
              <text class="photo-desc">{{photo.description}}</text>
              <view class="photo-meta">
                <text class="collect-time">{{photo.collectTime}}</text>
                <view class="photo-author">
                  <image class="author-avatar-small" :src="photo.authorAvatar"></image>
                  <text>{{photo.authorName}}</text>
                </view>
              </view>
            </view>
          </view>
        </view>
        <view class="waterfall-column">
          <view 
            class="photo-item" 
            v-for="photo in rightPhotos" 
            :key="photo.id"
            @click="previewPhoto(photo)"
          >
            <image :src="photo.url" mode="widthFix"></image>
            <view class="photo-info">
              <text class="photo-desc">{{photo.description}}</text>
              <view class="photo-meta">
                <text class="collect-time">{{photo.collectTime}}</text>
                <view class="photo-author">
                  <image class="author-avatar-small" :src="photo.authorAvatar"></image>
                  <text>{{photo.authorName}}</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
      
      <!-- 加载更多提示 -->
      <view class="loading-more" v-if="isLoading">
        <u-loading-icon mode="circle" size="24" color="#999"></u-loading-icon>
        <text>加载中...</text>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 'albums', // 当前激活的标签
      isRefreshing: false,
      isLoading: false,
      favoriteAlbums: [
        {
          id: 1,
          name: '春日樱花',
          cover: '/static/src.png',
          photoCount: 36,
          collectTime: '2024-03-15',
          authorAvatar: '/static/src.png',
          authorName: '摄影师小王'
        },
        {
          id: 2,
          name: '城市夜景',
          cover: '/static/src.png',
          photoCount: 24,
          collectTime: '2024-03-14',
          authorAvatar: '/static/src.png',
          authorName: '夜景达人'
        }
        // 更多相册数据...
      ],
      leftPhotos: [
        {
          id: 1,
          url: '/static/src.png',
          description: '春日樱花',
          collectTime: '2024-03-15',
          authorAvatar: '/static/src.png',
          authorName: '摄影师小王'
        }
        // 更多照片数据...
      ],
      rightPhotos: [
        {
          id: 2,
          url: '/static/src.png',
          description: '城市夜景',
          collectTime: '2024-03-14',
          authorAvatar: '/static/src.png',
          authorName: '夜景达人'
        }
        // 更多照片数据...
      ]
    }
  },
  methods: {
    // 下拉刷新
    async onRefresh() {
      this.isRefreshing = true;
      await this.loadData();
      this.isRefreshing = false;
    },
    
    // 加载更多
    loadMore() {
      if (this.isLoading) return;
      this.isLoading = true;
      
      // 模拟加载更多数据
      setTimeout(() => {
        this.isLoading = false;
      }, 1000);
    },
    
    // 打开相册
    openAlbum(album) {
      uni.navigateTo({
        url: `/pages/album/detail?id=${album.id}`
      });
    },
    
    // 预览照片
    previewPhoto(photo) {
      uni.previewImage({
        urls: [photo.url],
        current: photo.url
      });
    },
    
    // 加载数据
    async loadData() {
      // 模拟加载数据
      await new Promise(resolve => setTimeout(resolve, 1000));
    }
  }
}
</script>

<style>
.favorites-container {
  min-height: 100vh;
  background-color: #f8f8f8;
  padding-bottom: 30rpx;  /* 添加底部内边距 */
}

/* 顶部切换栏样式 */
.tab-header {
  display: flex;
  background-color: #fff;
  padding: 20rpx 40rpx;
  position: sticky;
  top: var(--window-top);  /* 适配导航栏高度 */
  z-index: 100;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.tab-item {
  flex: 1;
  text-align: center;
  position: relative;
  padding: 20rpx 0;
}

.tab-item text {
  font-size: 28rpx;
  color: #666;
  transition: color 0.3s;
}

.tab-item.active text {
  color: #3a8838;
  font-weight: bold;
}

.tab-line {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 40rpx;
  height: 4rpx;
  background-color: #3a8838;
  border-radius: 2rpx;
}

/* 内容区域滚动容器 */
.content-scroll {
  height: calc(100vh - 88rpx - var(--window-top));  /* 减去导航栏高度 */
}

/* 相册网格样式 */
.albums-grid {
  padding: 20rpx;
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
}

.album-item {
  width: calc(50% - 10rpx);
  background-color: #fff;
  border-radius: 12rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.album-cover {
  width: 100%;
  height: 200rpx;
  background-color: #f5f5f5;
}

.album-info {
  padding: 15rpx;
}

.album-name {
  font-size: 28rpx;
  color: #333;
  font-weight: 500;
  margin-bottom: 8rpx;
}

.album-meta {
  display: flex;
  justify-content: space-between;
  font-size: 24rpx;
  color: #999;
}

.album-author {
  display: flex;
  align-items: center;
  padding: 15rpx;
  border-top: 1px solid #f5f5f5;
}

.author-avatar {
  width: 40rpx;
  height: 40rpx;
  border-radius: 20rpx;
  margin-right: 10rpx;
}

.author-name {
  font-size: 24rpx;
  color: #666;
}

/* 照片瀑布流样式 */
.photos-waterfall {
  padding: 20rpx;
  display: flex;
  gap: 20rpx;
}

.waterfall-column {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.photo-item {
  background-color: #fff;
  border-radius: 12rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.photo-item image {
  width: 100%;
  height: auto;
}

.photo-info {
  padding: 12rpx 15rpx;
}

.photo-desc {
  font-size: 26rpx;
  color: #333;
  margin-bottom: 12rpx;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.photo-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 24rpx;
  color: #999;
  padding-top: 10rpx;
  border-top: 1px solid #f5f5f5;
}

.photo-author {
  display: flex;
  align-items: center;
  flex: 1;
}

.photo-author text {
  font-size: 24rpx;
  color: #666;
  margin-left: 8rpx;
  max-width: 120rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.collect-time {
  font-size: 22rpx;
  color: #999;
  margin-left: auto;
  padding-left: 10rpx;
}

.author-avatar-small {
  width: 32rpx;
  height: 32rpx;
  border-radius: 16rpx;
  margin-right: 6rpx;
}

/* 加载更多样式 */
.loading-more {
  text-align: center;
  padding: 30rpx;
  color: #999;
  font-size: 24rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10rpx;
}
</style> 